package com.dingding.service.activity.core.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.IntegralActivity;
import com.dingding.service.activity.core.dao.IntegralActivityDao;
import com.dingding.service.activity.core.sql.IntegralActivitySQL;

/**
 * 
 * @ClassName: IntegralActivityDao 
 * @Description: 积分优惠活动表DAO
 * @author CHEHAUNBO 
 * @date 2016年4月1日 上午9:47:37
 * 
 */
@Component("integralActivityDao")
public class IntegralActivityDaoImpl extends BaseDaoImpl<IntegralActivity> implements IntegralActivityDao {

	private static final Logger log = Logger.getLogger(IntegralActivityDaoImpl.class);

	/**
	 * 
	 * @Title: getIntegralActivityList 
	 * @Description: 获取所有的积分优惠活动列表
	 * @return List<IntegralActivity> 所有积分优惠活动列表
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public List<IntegralActivity> getIntegralActivityList() {

		// 优惠活动列表
		List<IntegralActivity> integralActivities = null;

		// 添加获取所有的积分优惠活动列表的查询条件(1:有效 2:无效)
		Criterion criterion = Restrictions.eq("activityStatus", 1);

		try {
			integralActivities = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("获取所有的积分优惠活动列表出现异常！", e);
		}
		return integralActivities;

	}

	/**
	 * @Title: resetInvalidActivity
	 * @Description: 重置积分优惠券活动表
	 * @author yuHaiQing
	 * @return boolean    true:重置成功  false:重置失败
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean resetInvalidIntegralActivity() {

		// 将过期的积分优惠券活动改为无效,并返回受影响的行数
		int row = -1;
		try {
			row = super.updateSqlRow(IntegralActivitySQL.RESET_INVALID_INTEGRAL_ACTIVITY);
		} catch (Exception e) {
			log.error("重置积分优惠券活动时出现异常   类：" + this.getClass() + " resetInvalidIntegralActivity()", e);
		}

		// 判断是否重置成功，如果小于0，修改失败
		if (row < 0) {
			return false;
		}

		return true;
	}

	/**
	 * @Title: resetActivityLimitNumber
	 * @Description: 重置活动限制次数（如果该活动参数次数按每天参与次数的话）
	 * @author yuHaiQing
	 * @return boolean true:重置成功  false:重置失败
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean resetActivityLimitNumber() {

		// 将过期的积分优惠券活动改为无效,并返回受影响的行数
		int row = -1;
		try {
			row = super.updateSqlRow(IntegralActivitySQL.RESET_ACTIVITY_LIMIT_NUMBER);
		} catch (Exception e) {
			log.error("重置活动限制次数时出现异常   类：" + this.getClass() + " resetActivityLimitNumber()", e);
		}

		// 判断是否重置成功，如果小于0，修改失败
		if (row < 0) {
			return false;
		}

		return true;
	}

}
